/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easyanimation.panel;

import easyanimation.frame.CConfig;
import easyanimation.frame.Constant;
import easyanimation.frame.Language;
import easyanimation.frame.MainWindow;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.Timer;

/**
 *
 * @author Ngoc Tan
 */
public class CCenterPanel extends JPanel
{
    //image    
    private Image m_image = null;
    private int m_zoomValueI = Constant.ZOOM_NORMAL;//value -> %
    private int m_mouseXI = 0, m_mouseYI = 0;//mouse point
    private int m_mousePressedXI = 0, m_mousePressedYI = 0;//mouse point
    private int m_mouseCursorI = Constant.CURSOR_NORMAL;//mouse cursor
    //rectangles for module
    private int[] m_rectInfoSaveIs = null;//for store the data of table for process mouse resizing
    private int[] m_rectInfoIs = null;  //for the data using to process
    private int[] m_rectInfoNormalIs = null;  //for the data at zoom == 100% (no zoom)
    //for frame
    private Vector<Vector<int[]>> m_framesVector = null;//each element is a set of index-modules, each element of this set is a index of a module
    //for animation
    private Vector<Vector<int[]>> m_animationsVector = null;//each element is a set of index-frames, each element of this set is a index of a frame    
    private boolean m_isPaintAnimation = true;//paint animations (true) or frame (false)
    private int m_frameCurrentPaintI = 0;//the current frame paint in painting animations
    private int m_frameCurrentLoopPaintI = 0;//the current loop in a frame in painting animations
    private Timer m_timer;
    //size of this
    private Dimension m_centerZoomDimension;

    public CCenterPanel()
    {
        m_centerZoomDimension = new Dimension(Constant.WINDOW_WIDTH - Constant.WINDOW_WIDTH_LEFT - Constant.WINDOW_WIDTH_RIGHT,
                Constant.WINDOW_HEIGHT - Constant.WINDOW_HEIGHT_HEADER - Constant.WINDOW_WIDTH_BOTTOM);
        setSize(m_centerZoomDimension);
        setBorder(Constant.OBJ_BORDER_COMMON);
        setBackground(Color.WHITE);
        //setLayout(null);//remove layout manager
        //init the image
        m_image = null;
        //add mouse listener
        addMouseWheelListener(new MouseWheelListener()
        {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e)
            {
                if (e.getWheelRotation() < 0) //up
                {
                    m_zoomValueI += Constant.ZOOM_STEP;
                    if (m_zoomValueI > Constant.ZOOM_MAX)
                    {
                        m_zoomValueI = Constant.ZOOM_MAX;
                    }
                }
                else//down
                {
                    m_zoomValueI -= Constant.ZOOM_STEP;
                    if (m_zoomValueI < Constant.ZOOM_MIN)
                    {
                        m_zoomValueI = Constant.ZOOM_MIN;
                    }
                }
                setPreferredSize(m_centerZoomDimension);
                processScale(e.getX(), e.getY());
                getStatusPanel().setZoomForStatus(m_zoomValueI);
                reupdateUI();
            }
        });
        addMouseListener(new MouseListener()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                m_mousePressedXI = scaleAndTranslate(e.getX(), m_centerZoomDimension.width);
                m_mousePressedYI = scaleAndTranslate(e.getY(), m_centerZoomDimension.height);
            }

            @Override
            public void mouseClicked(MouseEvent e)
            {
            }

            @Override
            public void mouseReleased(MouseEvent e)
            {
                //m_iMouseReleasedX = scaleAndTranslate(e.getX(), m_objCenterZoomSize.width);
                //m_iMouseReleasedY = scaleAndTranslate(e.getY(), m_objCenterZoomSize.height);

                switch (getLeftPanel().getSelectedIndex())
                {
                    case Constant.TAB_INDEX_PALETTE:
                    {
                    }
                    break;

                    case Constant.TAB_INDEX_MODULE:
                    {
                        JTable moduleTable = getLeftPanel().getTabModule().getTable();
                        int iRow = moduleTable.getSelectedRow();
                        if (iRow > -1)
                        {
                            saveToTable(moduleTable, iRow, Constant.COLUMN_TAB_MODULE_X, Constant.MODULE_INFO_X);
                            saveToTable(moduleTable, iRow, Constant.COLUMN_TAB_MODULE_Y, Constant.MODULE_INFO_Y);
                            saveToTable(moduleTable, iRow, Constant.COLUMN_TAB_MODULE_W, Constant.MODULE_INFO_W);
                            saveToTable(moduleTable, iRow, Constant.COLUMN_TAB_MODULE_H, Constant.MODULE_INFO_H);
                            moduleTable.updateUI();
                        }
                    }
                    break;

                    case Constant.TAB_INDEX_FRAME:
                    {
                        JTable moduleTable = getLeftPanel().getSubTableModuleFrame().getTable();
                        int rowSubTableSelectI = moduleTable.getSelectedRow();
                        int rowFrameSelectI = getLeftPanel().getTabFrame().getTable().getSelectedRow();
                        if (rowSubTableSelectI > -1)
                        {
                            saveToTable(moduleTable, rowSubTableSelectI, Constant.COLUMN_SUB_TABLE_X, Constant.MODULE_INFO_X);
                            m_framesVector.elementAt(rowFrameSelectI).elementAt(rowSubTableSelectI)[Constant.COLUMN_SUB_TABLE_X] = m_rectInfoNormalIs[Constant.MODULE_INFO_X] = descale(m_rectInfoIs[Constant.MODULE_INFO_X]);
                            saveToTable(moduleTable, rowSubTableSelectI, Constant.COLUMN_SUB_TABLE_Y, Constant.MODULE_INFO_Y);
                            m_framesVector.elementAt(rowFrameSelectI).elementAt(rowSubTableSelectI)[Constant.COLUMN_SUB_TABLE_Y] = m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = descale(m_rectInfoIs[Constant.MODULE_INFO_Y]);
                            moduleTable.updateUI();
                        }
                    }

                    break;
                    case Constant.TAB_INDEX_ANIMATION:
                    {
                    }
                    break;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e)
            {
            }

            @Override
            public void mouseExited(MouseEvent e)
            {
            }
        });
        //add mouse motion listener
        addMouseMotionListener(new MouseMotionListener()
        {
            @Override
            public void mouseDragged(MouseEvent e)
            {
                //scale
                processScale(e.getX(), e.getY());
                //resize the module
                //change cursor
                switch (getLeftPanel().getSelectedIndex())
                {
                    case Constant.TAB_INDEX_PALETTE:
                    {
                    }
                    break;

                    case Constant.TAB_INDEX_MODULE:
                    {
                        if (getLeftPanel().getTabModule().getTable().getRowCount() < 1)
                        {
                            break;
                        }
                        updateResize(false);
                    }
                    break;

                    case Constant.TAB_INDEX_FRAME:
                    {
                        if (getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() < 1)
                        {
                            break;
                        }
                        updateResize(true);
                    }

                    break;
                    case Constant.TAB_INDEX_ANIMATION:
                    {
                        if (getLeftPanel().getSubTableFrameAnimation().getTable().getRowCount() < 1)
                        {
                            break;
                        }
                        //updateResize(true);
                    }
                    break;
                }
            }

            @Override
            public void mouseMoved(MouseEvent e)
            {
                //scale
                processScale(e.getX(), e.getY());
                //change cursor
                switch (getLeftPanel().getSelectedIndex())
                {
                    case Constant.TAB_INDEX_PALETTE:
                    {
                    }
                    break;

                    case Constant.TAB_INDEX_MODULE:
                    {
                        if (m_rectInfoIs != null)
                        {
                            updateCursor(new Rectangle(m_rectInfoIs[0], m_rectInfoIs[1], m_rectInfoIs[2], m_rectInfoIs[3]), false);
                        }
                    }
                    break;

                    case Constant.TAB_INDEX_FRAME:
                    {
                        if (m_framesVector != null && !m_framesVector.isEmpty())
                        {
                            int rowFrameSelectI = getLeftPanel().getTabFrame().getTable().getSelectedRow();
                            if (!m_framesVector.elementAt(rowFrameSelectI).isEmpty())
                            {
                                int rowSubTableSelectI = getLeftPanel().getSubTableModuleFrame().getTable().getSelectedRow();
                                if (rowSubTableSelectI != -1 && rowSubTableSelectI < m_framesVector.elementAt(rowFrameSelectI).size())
                                {
                                    int[] moduleIs = m_framesVector.elementAt(rowFrameSelectI).elementAt(rowSubTableSelectI);
                                    Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(m_framesVector.elementAt(rowFrameSelectI).elementAt(rowSubTableSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                                    updateCursor(new Rectangle(moduleIs[Constant.COLUMN_SUB_TABLE_X],
                                            moduleIs[Constant.COLUMN_SUB_TABLE_Y],
                                            Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]), // the x coordinate of the second corner of the destination rectangle.
                                            Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H])),
                                            true);
                                }
                            }
                        }
                    }

                    break;
                    case Constant.TAB_INDEX_ANIMATION:
                    {
                    }
                    break;
                }
            }
        });
    }

    public int scaleAndTranslate(int iMouse, int iDimension)
    {
        iMouse -= iDimension >> 1;
        return scale(iMouse);
    }

    public int scale(int iSize)
    {
        return (int) (iSize * m_zoomValueI / 100.0);
    }

    public int descale(int iSize)
    {
        return (int) (iSize * 100.0 / m_zoomValueI);
    }

    public void saveToTable(JTable table, int iRow, int iColumn, int iPos)
    {
        m_rectInfoNormalIs[iPos] = descale(m_rectInfoIs[iPos]);
        table.setValueAt(
                "" + m_rectInfoNormalIs[iPos],
                iRow, iColumn);
        m_rectInfoSaveIs[iPos] = m_rectInfoIs[iPos];
    }

    public void processScale(int iMouseX, int iMouseY)
    {
        m_mouseXI = iMouseX - (m_centerZoomDimension.width >> 1);
        m_mouseYI = iMouseY - (m_centerZoomDimension.height >> 1);
        getStatusPanel().setMouseView(m_mouseXI, m_mouseYI);
        m_mouseXI = scale(m_mouseXI);
        m_mouseYI = scale(m_mouseYI);
        if (m_rectInfoIs != null)
        {
            for (int i = 0; i < Constant.TOTAL_MODULE_INFOS; i++)
            {
                m_rectInfoIs[i] = m_rectInfoSaveIs[i] = scale(m_rectInfoNormalIs[i]);
            }
        }

    }

    @Override
    public void paint(Graphics g)
    {
        //clear panel
        g.setColor(Color.WHITE);
        int multi = 20;
        g.fillRect(0, 0, multi * m_centerZoomDimension.width, multi * m_centerZoomDimension.height);
        //scale with use graphic 2D        
        ((Graphics2D) g).translate(m_centerZoomDimension.width >> 1, m_centerZoomDimension.height >> 1);
        ((Graphics2D) g).scale(m_zoomValueI / 100.0, m_zoomValueI / 100.0);
        //draw background of coordinate
        paintCoordinateBackground(g, m_centerZoomDimension.width, m_centerZoomDimension.height);

        switch (getLeftPanel().getSelectedIndex())
        {
            case Constant.TAB_INDEX_PALETTE:
            {
                //draw image        
                if (m_image != null)
                {
                    g.drawImage(m_image, 0, 0, this);
                }
            }
            break;
            case Constant.TAB_INDEX_MODULE:
            {
                //draw image        
                if (m_image != null)
                {
                    g.drawImage(m_image, 0, 0, this);
                }
                if (getLeftPanel().getTabModule().getTable().getRowCount() > 0)
                {
                    g.setColor(Color.RED);
                    g.drawRect(m_rectInfoIs[Constant.MODULE_INFO_X],
                            m_rectInfoIs[Constant.MODULE_INFO_Y],
                            m_rectInfoIs[Constant.MODULE_INFO_W],
                            m_rectInfoIs[Constant.MODULE_INFO_H]);
                }
                //m_imagePanel.paintComponent(g);
            }
            break;

            case Constant.TAB_INDEX_FRAME:
            {
                if (m_framesVector != null && !m_framesVector.isEmpty())
                {
                    int rowFrameSelectI = getLeftPanel().getTabFrame().getTable().getSelectedRow();
                    int rowSubTableSelectI = getLeftPanel().getSubTableModuleFrame().getTable().getSelectedRow();
                    if (getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() < 1)
                    {
                        break;
                    }
                    Vector<int[]> modulesVector = m_framesVector.elementAt(rowFrameSelectI);
                    for (int i = 0; i < modulesVector.size(); i++)
                    {
                        //get the module
                        Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                        //draw module
                        if (rowSubTableSelectI == i)
                        {
                            paintModule(g,
                                    m_rectInfoIs[Constant.MODULE_INFO_X],
                                    m_rectInfoIs[Constant.MODULE_INFO_Y],
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_Y]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]));
                        }
                        else
                        {
                            paintModule(g,
                                    modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_X],
                                    modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_Y],
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_Y]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]));
                        }
                    }
                    if (getLeftPanel().getSubTableModuleFrame().getTable().getSelectedRow() > -1
                            && getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() > 0)
                    {
                        g.setColor(Color.RED);
                        g.drawRect(m_rectInfoIs[Constant.MODULE_INFO_X],
                                m_rectInfoIs[Constant.MODULE_INFO_Y],
                                m_rectInfoIs[Constant.MODULE_INFO_W],
                                m_rectInfoIs[Constant.MODULE_INFO_H]);
                    }
                }
            }
            break;

            case Constant.TAB_INDEX_ANIMATION:
            {
                if (m_animationsVector != null && !m_animationsVector.isEmpty())
                {
                    if (getLeftPanel().getSubTableFrameAnimation().getTable().getRowCount() < 1)
                    {
                        if (m_timer != null)
                        {
                            m_timer.stop();
                            m_timer = null;
                        }
                        break;
                    }
                    if (m_isPaintAnimation)
                    {                        
                        int rowAnimationSelectI = getLeftPanel().getTabAnimation().getTable().getSelectedRow();
                        int[] frameInfoIs = m_animationsVector.elementAt(rowAnimationSelectI).elementAt(m_frameCurrentPaintI);
                        Vector<int[]> modulesVector = m_framesVector.elementAt(frameInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                        for (int i = 0; i < modulesVector.size(); i++)
                        {
                            //get the module
                            Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                            paintModule(g,
                                    modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_X],
                                    modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_Y],
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_Y]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]));

                        }
                        if (m_timer == null)
                        {
                            m_timer = new Timer(CConfig.TIME_FOR_FRAME, new PaintListener());
                            m_timer.start();
                        }
                    }
                    else
                    {
                        if (m_timer != null)
                        {
                            m_timer.stop();
                            m_timer = null;
                        }
                        int rowAnimationSelectI = getLeftPanel().getTabAnimation().getTable().getSelectedRow();
                        int rowSubTableSelectI = getLeftPanel().getSubTableFrameAnimation().getTable().getSelectedRow();
                        Vector<int[]> framesVector = m_animationsVector.elementAt(rowAnimationSelectI);
                        int[] frameInfoIs = framesVector.elementAt(rowSubTableSelectI);
                        Vector<int[]> modulesVector = m_framesVector.elementAt(frameInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                        for (int i = 0; i < modulesVector.size(); i++)
                        {
                            //get the module
                            Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                            paintModule(g,
                                    modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_X],
                                    modulesVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_Y],
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_Y]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]));

                        }
                    }
                }
            }
            break;
        }
    }

    /**
     * paint background for coordinate
     */
    private void paintCoordinateBackground(Graphics g, int width, int height)
    {
        //------------------paint the axis of coordinate------------------
        g.setColor(Color.GRAY);
        //g.drawLine(m_xStep * X_MAX, 0, m_xStep * X_MAX, height);
        //g.drawLine(0, m_yStep * Y_MAX, width, m_yStep * Y_MAX);
        //int scaleUpValue = (m_zoomValue < 100) ? (int) (100.0 / m_zoomValue) : 1;
        //g.drawLine(0, -height * scaleUpValue, 0, height * scaleUpValue);
        //g.drawLine(-width * scaleUpValue, 0, width * scaleUpValue, 0);
        int multi = 100;
        g.drawLine(0, -height * multi, 0, height * multi);
        g.drawLine(-width * multi, 0, width * multi, 0);

        //------------------paint the grid------------------
        //g.setColor(Color.LIGHT_GRAY);
        //int temp = 0, count = 1;
        //while (temp < width)
        //{
        //    if (temp != xStep * X_MAX)
        //    {
        //        g.drawLine(temp, 0, temp, height);
        //    }
        //    temp = xStep * count++;
        //}
        //temp = 0;
        //count = 1;
        //while (temp < height)
        //{
        //    if (temp != yStep * Y_MAX)
        //    {
        //        g.drawLine(0, temp, width, temp);
        //    }
        //    temp = yStep * count++;
        //}
    }

    /**
     * paint a module
     */
    private void paintModule(Graphics g, int x, int y, int xImage, int yImage, int width, int height)
    {
        g.drawImage(m_image,
                x, //x coordinate of the first corner of the destination rectangle
                y, //y coordinate of the first corner of the destination rectangle
                x + width, //w coordinate of the first corner of the destination rectangle
                y + height, //h coordinate of the first corner of the destination rectangle
                xImage, //the x coordinate of the first corner of the source rectangle
                yImage, //the y coordinate of the first corner of the source rectangle
                xImage + width, //the w coordinate of the first corner of the source rectangle
                yImage + height, //the h coordinate of the first corner of the source rectangle
                this);
    }

    private void paintModule(Graphics g, int x, int y, int moduleTableRowI)
    {
        //get the module
        Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(moduleTableRowI);
        //draw module
        paintModule(g, x, y,
                Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X]),
                Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_Y]),
                Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]),
                Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]));

    }

    /**
     * set the image from path
     * @param imagePath 
     */
    public void setImage(String imagePath)
    {
        m_image = Toolkit.getDefaultToolkit().getImage(imagePath); //only support .jpg, .png, .gif
        if (m_image == null)
        {
            JOptionPane.showMessageDialog(MainWindow.getInstance(), Language.LOAD_IMAGE_FAIL[CConfig.LANGUAGE], Language.ERROR[CConfig.LANGUAGE], JOptionPane.ERROR_MESSAGE);
            return;
        }
        getRightPanel().setImage(m_image);
        reupdateUI();
    }

    @Override
    public Dimension getPreferredSize()
    {
        int w = getCenterSize().width;
        int h = getCenterSize().height;
        return new Dimension(scale(w), scale(h));
    }

    /**
     * update UI
     */
    public void reupdateUI()
    {
        m_centerZoomDimension.width = scale(getCenterSize().width);
        m_centerZoomDimension.height = scale(getCenterSize().height);
        setScrollToRectVisible();
        updateUI();
    }

    public void updateFromTable(boolean isSubTable, int actionI, int rowSelectI, int[] itemInfoIs)
    {
        switch (getLeftPanel().getSelectedIndex())
        {
            case Constant.TAB_INDEX_PALETTE:
            {
            }
            break;
            case Constant.TAB_INDEX_MODULE:
            {
                if (m_rectInfoIs == null)
                {
                    m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
                    m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
                    m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];
                }
                //load the info of the focusing module
                if (getLeftPanel().getTabModule().getTable().getRowCount() > 0)
                {
                    Object[] m_objArrModuleInfo = getLeftPanel().getTabModule().getRow(getLeftPanel().getTabModule().getTable().getSelectedRow());
                    for (int i = 0; i < Constant.TOTAL_MODULE_INFOS; i++)
                    {
                        m_rectInfoNormalIs[i] = m_rectInfoSaveIs[i] = m_rectInfoIs[i] = Integer.parseInt((String) m_objArrModuleInfo[Constant.COLUMN_TAB_MODULE_X + i]);
                    }
                }
                for (int i = 0; i < Constant.TOTAL_MODULE_INFOS; i++)
                {
                    if (m_zoomValueI == Constant.ZOOM_NORMAL)
                    {
                        m_rectInfoIs[i] = m_rectInfoSaveIs[i] = m_rectInfoNormalIs[i];
                    }
                    else
                    {
                        m_rectInfoIs[i] = m_rectInfoSaveIs[i] = scale(m_rectInfoNormalIs[i]);
                    }
                }
            }
            break;

            case Constant.TAB_INDEX_FRAME:
            {
                if (isSubTable)
                {
                    int iRowFrameSelect = getLeftPanel().getTabFrame().getTable().getSelectedRow();
                    if (actionI == Constant.TABLE_ACTION_ADD
                            || (actionI == Constant.TABLE_ACTION_INSERT && getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() == 1))
                    {
                        //get the recently adding row
                        Object[] moduleInfoObjects = getLeftPanel().getSubTableModuleFrame().getRow(getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() - 1);
                        //add to the vector
                        m_framesVector.elementAt(iRowFrameSelect).add(new int[]
                                {
                                    ((Integer) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_INDEX]).intValue(),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_X]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_Y])
                                });
                        m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
                        m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
                        m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];

                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_X]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_Y]);
                        moduleInfoObjects = getLeftPanel().getTabModule().getRow(Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]));
                        m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                    }
                    else if (actionI == Constant.TABLE_ACTION_INSERT)
                    {
                        //get the recently adding row
                        Object[] moduleInfoObjects = getLeftPanel().getSubTableModuleFrame().getRow(getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() - 1);
                        //insert to the vector
                        m_framesVector.elementAt(iRowFrameSelect).insertElementAt(new int[]
                                {
                                    ((Integer) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_INDEX]).intValue(),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_X]),
                                    Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_Y])
                                }, rowSelectI + 1);
                    }
                    else if (actionI == Constant.TABLE_ACTION_DELETE)
                    {
                        m_framesVector.elementAt(iRowFrameSelect).removeElementAt(rowSelectI);
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE)
                    {
                        //get the recently adding row
                        Object[] moduleInfoObjects = getLeftPanel().getSubTableModuleFrame().getRow(rowSelectI);
                        //insert to the vector
                        m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_INDEX] = ((Integer) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_INDEX]).intValue();
                        m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_X] = m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_X] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_X]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_Y] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_SUB_TABLE_Y]);
                    }
                    else if (actionI == Constant.TABLE_ACTION_ADD_OUTSIDE)
                    {
                        //add to the vertor
                        m_framesVector.elementAt(iRowFrameSelect).add(itemInfoIs);
                        //add to the table                        
                        Object[] dataObjects = getLeftPanel().getSubTableModuleFrame().getDataDefaultObjects();
                        dataObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = "" + itemInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX];
                        dataObjects[Constant.COLUMN_SUB_TABLE_X] = "" + itemInfoIs[Constant.COLUMN_SUB_TABLE_X];
                        dataObjects[Constant.COLUMN_SUB_TABLE_Y] = "" + itemInfoIs[Constant.COLUMN_SUB_TABLE_Y];
                        getLeftPanel().getSubTableModuleFrame().getTableModel().addRow(dataObjects);
                        getLeftPanel().getSubTableModuleFrame().getTable().updateUI();
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE_INDEX)//update the module index
                    {
                        //add to the vertor
                        m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = itemInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX];
                        //add to the table                        
                        getLeftPanel().getSubTableModuleFrame().getTable().setValueAt(itemInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] + "", rowSelectI, Constant.COLUMN_SUB_TABLE_ITEM_INDEX);
                        getLeftPanel().getSubTableModuleFrame().getTable().updateUI();
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE_NOT_INDEX)
                    {
                        //update the module not index
                    }
                    else if (actionI == Constant.TABLE_ACTION_SELECT)
                    {
                        m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
                        m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
                        m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];

                        Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_X];
                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = m_framesVector.elementAt(iRowFrameSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_Y];
                        m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                    }

                }
                else //add a new row or insert the first row
                {
                    if (actionI == Constant.TABLE_ACTION_ADD
                            || actionI == Constant.TABLE_ACTION_INSERT && getLeftPanel().getTabFrame().getTable().getRowCount() == 1)
                    {
                        if (m_framesVector == null)
                        {
                            m_framesVector = new Vector<Vector<int[]>>();
                        }
                        m_framesVector.add(new Vector<int[]>());
                        getLeftPanel().getSubTableModuleFrame().getTableModel().removeAllRow();
                        getLeftPanel().getSubTableModuleFrame().getTable().updateUI();
                    }
                    else if (actionI == Constant.TABLE_ACTION_INSERT)
                    {
                        m_framesVector.insertElementAt(new Vector<int[]>(), rowSelectI + 1);
                    }
                    else if (actionI == Constant.TABLE_ACTION_DELETE)
                    {
                        m_framesVector.removeElementAt(rowSelectI);
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE)
                    {
                    }
                    else if (actionI == Constant.TABLE_ACTION_SELECT)
                    {
                        getLeftPanel().getSubTableModuleFrame().getTableModel().removeAllRow();
                        //getLeftPanel().getSubTableModuleFrame().getTable().changeSelection(-1, 0, false, false);;
                        //get number modules
                        Vector<int[]> tempVector = m_framesVector.elementAt(rowSelectI);
                        int totalModulesI = tempVector.size();
                        for (int i = 0; i < totalModulesI; i++)
                        {
                            Object[] dataObjects = getLeftPanel().getSubTableModuleFrame().getDataDefaultObjects();
                            dataObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = "" + tempVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX];
                            dataObjects[Constant.COLUMN_SUB_TABLE_X] = "" + tempVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_X];
                            dataObjects[Constant.COLUMN_SUB_TABLE_Y] = "" + tempVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_Y];
                            getLeftPanel().getSubTableModuleFrame().getTableModel().addRow(dataObjects);
                        }
                        getLeftPanel().getSubTableModuleFrame().getTable().updateUI();
                        if (totalModulesI > 0)
                        {
                            m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
                            m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
                            m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];

                            int[] moduleInfosIs = m_framesVector.elementAt(rowSelectI).elementAt(0);
                            Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(moduleInfosIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                            m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = moduleInfosIs[Constant.COLUMN_SUB_TABLE_X];
                            m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = moduleInfosIs[Constant.COLUMN_SUB_TABLE_Y];
                            m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
                            m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                        }
                    }
                }
                getLeftPanel().doLayout();
            }
            break;

            case Constant.TAB_INDEX_ANIMATION:
            {
                if (isSubTable)
                {
                    int iRowAnimationSelect = getLeftPanel().getTabAnimation().getTable().getSelectedRow();
                    if (actionI == Constant.TABLE_ACTION_ADD
                            || (actionI == Constant.TABLE_ACTION_INSERT && getLeftPanel().getSubTableFrameAnimation().getTable().getRowCount() == 1))
                    {
                        //get the recently adding row
                        Object[] frameInfoObjects = getLeftPanel().getSubTableFrameAnimation().getRow(getLeftPanel().getSubTableFrameAnimation().getTable().getRowCount() - 1);
                        //add to the vector
                        m_animationsVector.elementAt(iRowAnimationSelect).add(new int[]
                                {
                                    ((Integer) frameInfoObjects[Constant.COLUMN_SUB_TABLE_INDEX]).intValue(),
                                    Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]),
                                    Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_X]),
                                    Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_Y])
                                });
//                        m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
//                        m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
//                        m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];
//
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_X]);
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_Y]);
//                        frameInfoObjects = getLeftPanel().getTabModule().getRow(Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]));
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                    }
                    else if (actionI == Constant.TABLE_ACTION_INSERT)
                    {
                        //get the recently adding row
                        Object[] frameInfoObjects = getLeftPanel().getSubTableFrameAnimation().getRow(getLeftPanel().getSubTableFrameAnimation().getTable().getRowCount() - 1);
                        //insert to the vector
                        m_animationsVector.elementAt(iRowAnimationSelect).insertElementAt(new int[]
                                {
                                    ((Integer) frameInfoObjects[Constant.COLUMN_SUB_TABLE_INDEX]).intValue(),
                                    Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]),
                                    Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_X]),
                                    Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_Y])
                                }, rowSelectI + 1);
                    }
                    else if (actionI == Constant.TABLE_ACTION_DELETE)
                    {
                        m_animationsVector.elementAt(iRowAnimationSelect).removeElementAt(rowSelectI);
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE)
                    {
                        //get the recently adding row
                        Object[] frameInfoObjects = getLeftPanel().getSubTableFrameAnimation().getRow(rowSelectI);
                        //insert to the vector
                        m_animationsVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_INDEX] = ((Integer) frameInfoObjects[Constant.COLUMN_SUB_TABLE_INDEX]).intValue();
                        m_animationsVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = m_framesVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_X] = m_framesVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_X] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_X]);
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = m_framesVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_Y] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_SUB_TABLE_Y]);
                    }
                    else if (actionI == Constant.TABLE_ACTION_ADD_OUTSIDE)
                    {
                        //add to the vertor
                        m_animationsVector.elementAt(iRowAnimationSelect).add(itemInfoIs);
                        //add to the table                        
                        Object[] dataObjects = getLeftPanel().getSubTableFrameAnimation().getDataDefaultObjects();
                        dataObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = "" + itemInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX];
                        dataObjects[Constant.COLUMN_SUB_TABLE_X] = "" + itemInfoIs[Constant.COLUMN_SUB_TABLE_X];
                        dataObjects[Constant.COLUMN_SUB_TABLE_Y] = "" + itemInfoIs[Constant.COLUMN_SUB_TABLE_Y];
                        getLeftPanel().getSubTableFrameAnimation().getTableModel().addRow(dataObjects);
                        getLeftPanel().getSubTableFrameAnimation().getTable().updateUI();
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE_INDEX)//update the module index
                    {
                        //add to the vertor
                        m_animationsVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = itemInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX];
                        //add to the table                        
                        getLeftPanel().getSubTableFrameAnimation().getTable().setValueAt(itemInfoIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] + "", rowSelectI, Constant.COLUMN_SUB_TABLE_ITEM_INDEX);
                        getLeftPanel().getSubTableFrameAnimation().getTable().updateUI();
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE_NOT_INDEX)
                    {
                        //update the module not index
                    }
                    else if (actionI == Constant.TABLE_ACTION_SELECT)
                    {
//                        m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
//                        m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
//                        m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];
//
//                        Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(m_framesVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = m_framesVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_X];
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = m_framesVector.elementAt(iRowAnimationSelect).elementAt(rowSelectI)[Constant.COLUMN_SUB_TABLE_Y];
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
//                        m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                    }

                }
                else //add a new row or insert the first row
                {
                    if (actionI == Constant.TABLE_ACTION_ADD
                            || actionI == Constant.TABLE_ACTION_INSERT && getLeftPanel().getTabAnimation().getTable().getRowCount() == 1)
                    {
                        if (m_animationsVector == null)
                        {
                            m_animationsVector = new Vector<Vector<int[]>>();
                        }
                        m_animationsVector.add(new Vector<int[]>());
                        getLeftPanel().getSubTableFrameAnimation().getTableModel().removeAllRow();
                        getLeftPanel().getSubTableFrameAnimation().getTable().updateUI();
                    }
                    else if (actionI == Constant.TABLE_ACTION_INSERT)
                    {
                        m_animationsVector.insertElementAt(new Vector<int[]>(), rowSelectI + 1);
                    }
                    else if (actionI == Constant.TABLE_ACTION_DELETE)
                    {
                        m_animationsVector.removeElementAt(rowSelectI);
                    }
                    else if (actionI == Constant.TABLE_ACTION_UPDATE)
                    {
                    }
                    else if (actionI == Constant.TABLE_ACTION_SELECT)
                    {
                        getLeftPanel().getSubTableFrameAnimation().getTableModel().removeAllRow();
                        //get number modules
                        Vector<int[]> tempVector = m_animationsVector.elementAt(rowSelectI);
                        int totalFrameI = tempVector.size();
                        for (int i = 0; i < totalFrameI; i++)
                        {
                            Object[] dataObjects = getLeftPanel().getSubTableFrameAnimation().getDataDefaultObjects();
                            dataObjects[Constant.COLUMN_SUB_TABLE_ITEM_INDEX] = "" + tempVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX];
                            dataObjects[Constant.COLUMN_SUB_TABLE_X] = "" + tempVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_X];
                            dataObjects[Constant.COLUMN_SUB_TABLE_Y] = "" + tempVector.elementAt(i)[Constant.COLUMN_SUB_TABLE_Y];
                            getLeftPanel().getSubTableFrameAnimation().getTableModel().addRow(dataObjects);
                        }
                        getLeftPanel().getSubTableFrameAnimation().getTable().updateUI();
//                        if (totalFrameI > 0)
//                        {
//                            m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
//                            m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
//                            m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];
//
//                            int[] moduleInfosIs = m_animationsVector.elementAt(rowSelectI).elementAt(0);
//                            Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(moduleInfosIs[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
//                            m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = moduleInfosIs[Constant.COLUMN_SUB_TABLE_X];
//                            m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = moduleInfosIs[Constant.COLUMN_SUB_TABLE_Y];
//                            m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
//                            m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
//                        }
                    }
                }
                getLeftPanel().doLayout();
            }
            break;
        }
        reupdateUI();
    }

    /**
     * @return the m_zoomValue
     */
    public double getZoomValue()
    {
        return m_zoomValueI;
    }

    /**
     * @param m_zoomValue the m_zoomValue to set
     */
    public void setZoomForCenter(int iZoomValue)
    {
        m_zoomValueI = iZoomValue;
        m_mouseXI = scale(m_mouseXI);
        m_mouseYI = scale(m_mouseYI);
        reupdateUI();
    }

    /**
     * set the view go to the center
     */
    public void setScrollToRectVisible()
    {
        if (m_centerZoomDimension.width > getCenterSize().width)
        {
            scrollRectToVisible(new Rectangle((m_centerZoomDimension.width >> 1) + (getCenterSize().width >> 1),
                    (m_centerZoomDimension.height >> 1) + (getCenterSize().height >> 1),
                    0, 0));
        }
    }

    /**
     * reset the rect infos of a module
     */
    public void resetRectInfos()
    {
        m_rectInfoIs = null;
        m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];
        m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
        switch (getLeftPanel().getSelectedIndex())
        {
            case Constant.TAB_INDEX_PALETTE:
            case Constant.TAB_INDEX_ANIMATION:
            {
                m_rectInfoIs = null;
                m_rectInfoNormalIs = null;
                m_rectInfoSaveIs = null;
            }
            break;

            case Constant.TAB_INDEX_MODULE:
            {
                int iRow = getLeftPanel().getTabModule().getTable().getSelectedRow();
                if (iRow > -1 && getLeftPanel().getTabModule().getTable().getRowCount() > 0)
                {
                    //get the recently adding row
                    Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(iRow);
                    m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
                    m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
                    m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];

                    m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X]);
                    m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_Y]);
                    m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
                    m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                }
                else
                {
                    m_rectInfoIs = null;
                    m_rectInfoNormalIs = null;
                    m_rectInfoSaveIs = null;
                }
            }
            break;

            case Constant.TAB_INDEX_FRAME:
            {
                int rowFrameSelectI = getLeftPanel().getTabFrame().getTable().getSelectedRow();
                if (rowFrameSelectI > -1 && getLeftPanel().getTabFrame().getTable().getRowCount() > 0)
                {
                    int rowModuleSelectI = getLeftPanel().getSubTableModuleFrame().getTable().getSelectedRow();
                    if (rowModuleSelectI > -1
                            && getLeftPanel().getSubTableModuleFrame().getTable().getRowCount() > 0
                            && !m_framesVector.elementAt(rowFrameSelectI).isEmpty())
                    {
                        //get the recently adding row
                        Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(m_framesVector.elementAt(rowFrameSelectI).elementAt(rowModuleSelectI)[Constant.COLUMN_SUB_TABLE_ITEM_INDEX]);
                        m_rectInfoSaveIs = new int[Constant.TOTAL_MODULE_INFOS];
                        m_rectInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
                        m_rectInfoNormalIs = new int[Constant.TOTAL_MODULE_INFOS];

                        m_rectInfoNormalIs[Constant.MODULE_INFO_X] = m_rectInfoSaveIs[Constant.MODULE_INFO_X] = m_rectInfoIs[Constant.MODULE_INFO_X] = m_framesVector.elementAt(rowFrameSelectI).elementAt(rowModuleSelectI)[Constant.COLUMN_SUB_TABLE_X];
                        m_rectInfoNormalIs[Constant.MODULE_INFO_Y] = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] = m_rectInfoIs[Constant.MODULE_INFO_Y] = m_framesVector.elementAt(rowFrameSelectI).elementAt(rowModuleSelectI)[Constant.COLUMN_SUB_TABLE_Y];
                        m_rectInfoNormalIs[Constant.MODULE_INFO_W] = m_rectInfoSaveIs[Constant.MODULE_INFO_W] = m_rectInfoIs[Constant.MODULE_INFO_W] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_W]);
                        m_rectInfoNormalIs[Constant.MODULE_INFO_H] = m_rectInfoSaveIs[Constant.MODULE_INFO_H] = m_rectInfoIs[Constant.MODULE_INFO_H] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_H]);
                    }
                    else
                    {
                        m_rectInfoIs = null;
                        m_rectInfoNormalIs = null;
                        m_rectInfoSaveIs = null;
                    }
                }
                else
                {
                    m_rectInfoIs = null;
                    m_rectInfoNormalIs = null;
                    m_rectInfoSaveIs = null;
                }
            }

            break;
        }
        updateUI();
    }

    public void updateCursor(Rectangle rectangle, boolean isOnlyMove)
    {
        int x0 = rectangle.x;
        int y0 = rectangle.y;
        int w = rectangle.width;
        int h = rectangle.height;
        if (isOnlyMove)
        {
            if (m_mouseXI > x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI < x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI > y0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI < y0 + h - Constant.CURSOR_DISTANCE_IN)
            {
                m_mouseCursorI = Constant.CURSOR_CENTER;
            }
            else
            {
                m_mouseCursorI = Constant.CURSOR_NORMAL;
            }
        }
        else
        {
            if (m_mouseXI > x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI < x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI > y0 - Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI < y0 + Constant.CURSOR_DISTANCE_IN)
            {
                m_mouseCursorI = Constant.CURSOR_UP;
            }
            else if (m_mouseXI > x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI < x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI > y0 + h - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI < y0 + h + Constant.CURSOR_DISTANCE_OUT)
            {
                m_mouseCursorI = Constant.CURSOR_DOWN;
            }
            else if (m_mouseXI < x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI > x0 - Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI > y0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI < y0 + h - Constant.CURSOR_DISTANCE_IN)
            {
                m_mouseCursorI = Constant.CURSOR_LEFT;
            }
            else if (m_mouseXI > x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI < x0 + w + Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI > y0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI < y0 + h - Constant.CURSOR_DISTANCE_IN)
            {
                m_mouseCursorI = Constant.CURSOR_RIGHT;
            }
            else if (m_mouseXI <= x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI >= x0 - Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI <= y0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI >= y0 - Constant.CURSOR_DISTANCE_OUT)
            {
                m_mouseCursorI = Constant.CURSOR_CROSS_UP_LEFT;
            }
            else if (m_mouseXI >= x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI <= x0 + w + Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI <= y0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI >= y0 - Constant.CURSOR_DISTANCE_OUT)
            {
                m_mouseCursorI = Constant.CURSOR_CROSS_UP_RIGHT;
            }
            else if (m_mouseXI <= x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI >= x0 - Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI >= y0 + h - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI <= y0 + h + Constant.CURSOR_DISTANCE_OUT)
            {
                m_mouseCursorI = Constant.CURSOR_CROSS_DOWN_LEFT;
            }
            else if (m_mouseXI >= x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI <= x0 + w + Constant.CURSOR_DISTANCE_OUT
                    && m_mouseYI >= y0 + h - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI <= y0 + h + Constant.CURSOR_DISTANCE_OUT)
            {
                m_mouseCursorI = Constant.CURSOR_CROSS_DOWN_RIGHT;
            }
            else if (m_mouseXI > x0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseXI < x0 + w - Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI > y0 + Constant.CURSOR_DISTANCE_IN
                    && m_mouseYI < y0 + h - Constant.CURSOR_DISTANCE_IN)
            {
                m_mouseCursorI = Constant.CURSOR_CENTER;
            }
            else
            {
                m_mouseCursorI = Constant.CURSOR_NORMAL;
            }
        }
        setCursor(m_mouseCursorI);
    }

    public void updateResize(boolean isOnlyMove)
    {
        int iDistanceDraggedX = m_mouseXI - m_mousePressedXI,
                iDistanceDraggedY = m_mouseYI - m_mousePressedYI;
        if (isOnlyMove)
        {
            int iXNew = m_rectInfoSaveIs[Constant.MODULE_INFO_X] + iDistanceDraggedX,
                    iYNew = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] + iDistanceDraggedY;
            if (m_mouseCursorI == Constant.CURSOR_CENTER)
            {
                m_rectInfoIs[Constant.MODULE_INFO_X] = iXNew;
                m_rectInfoIs[Constant.MODULE_INFO_Y] = iYNew;
            }
        }
        else
        {
            int iXNew = m_rectInfoSaveIs[Constant.MODULE_INFO_X] + iDistanceDraggedX,
                    iYNew = m_rectInfoSaveIs[Constant.MODULE_INFO_Y] + iDistanceDraggedY,
                    iWNewOut = m_rectInfoSaveIs[Constant.MODULE_INFO_W] + iDistanceDraggedX,
                    iHNewOut = m_rectInfoSaveIs[Constant.MODULE_INFO_H] + iDistanceDraggedY,
                    iWNewIn = m_rectInfoSaveIs[Constant.MODULE_INFO_W] - iDistanceDraggedX,
                    iHNewIn = m_rectInfoSaveIs[Constant.MODULE_INFO_H] - iDistanceDraggedY;
            iWNewOut = (iWNewOut > 0) ? iWNewOut : Constant.CURSOR_DISTANCE_IN;
            iHNewOut = (iHNewOut > 0) ? iHNewOut : Constant.CURSOR_DISTANCE_IN;
            iWNewIn = (iWNewIn > 0) ? iWNewIn : Constant.CURSOR_DISTANCE_IN;
            iHNewIn = (iHNewIn > 0) ? iHNewIn : Constant.CURSOR_DISTANCE_IN;
            switch (m_mouseCursorI)
            {
                case Constant.CURSOR_CENTER:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_X] = iXNew;
                    m_rectInfoIs[Constant.MODULE_INFO_Y] = iYNew;
                }
                break;
                case Constant.CURSOR_CROSS_DOWN_LEFT:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_X] = iXNew;
                    m_rectInfoIs[Constant.MODULE_INFO_H] = iHNewOut;
                    m_rectInfoIs[Constant.MODULE_INFO_W] = iWNewIn;
                }
                break;
                case Constant.CURSOR_CROSS_DOWN_RIGHT:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_H] = iHNewOut;
                    m_rectInfoIs[Constant.MODULE_INFO_W] = iWNewOut;
                }
                break;
                case Constant.CURSOR_CROSS_UP_LEFT:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_X] = iXNew;
                    m_rectInfoIs[Constant.MODULE_INFO_Y] = iYNew;
                    m_rectInfoIs[Constant.MODULE_INFO_W] = iWNewIn;
                    m_rectInfoIs[Constant.MODULE_INFO_H] = iHNewIn;
                }
                break;
                case Constant.CURSOR_CROSS_UP_RIGHT:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_Y] = iYNew;
                    m_rectInfoIs[Constant.MODULE_INFO_W] = iWNewOut;
                    m_rectInfoIs[Constant.MODULE_INFO_H] = iHNewIn;
                }
                break;
                case Constant.CURSOR_DOWN:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_H] = iHNewOut;
                }
                break;
                case Constant.CURSOR_LEFT:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_X] = iXNew;
                    m_rectInfoIs[Constant.MODULE_INFO_W] = iWNewIn;
                }
                break;
                case Constant.CURSOR_RIGHT:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_W] = iWNewOut;
                }
                break;
                case Constant.CURSOR_UP:
                {
                    m_rectInfoIs[Constant.MODULE_INFO_Y] = iYNew;
                    m_rectInfoIs[Constant.MODULE_INFO_H] = iHNewIn;
                }
                break;
            }
        }
        updateUI();
    }

    public void setCursor(int iCurrentCursor)
    {
        switch (m_mouseCursorI)
        {
            case Constant.CURSOR_NORMAL:
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                break;
            case Constant.CURSOR_CENTER:
                setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                break;
            case Constant.CURSOR_CROSS_DOWN_LEFT:
                setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_CROSS_DOWN_RIGHT:
                setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_CROSS_UP_LEFT:
                setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_CROSS_UP_RIGHT:
                setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_DOWN:
                setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_LEFT:
                setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_RIGHT:
                setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                break;
            case Constant.CURSOR_UP:
                setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                break;
        }
    }

    public CStatusPanel getStatusPanel()
    {
        return MainWindow.getInstance().getStatusPanel();
    }

    public CLeftPanel getLeftPanel()
    {
        return MainWindow.getInstance().getLeftPanel();
    }

    public Dimension getCenterSize()
    {
        return MainWindow.getInstance().getCenterSize();
    }

    public CRightPanel getRightPanel()
    {
        return MainWindow.getInstance().getRightPanel();
    }

    private class PaintListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (++m_frameCurrentPaintI >= m_animationsVector.elementAt(getLeftPanel().getTabAnimation().getTable().getSelectedRow()).size())
            {
                m_frameCurrentPaintI = 0;
            }
            reupdateUI();
        }
    }
} 
